<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="visible"
    width="600px"
    append-to-body
  >
    <el-form ref="form" :model="form" label-width="auto" :rules="rules">      

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="研讨会名称" prop="name" class="person">
            <el-input
              v-model="form.name"
              placeholder="研讨会名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="会议编号" prop="code" class="person">
            <el-input
              v-model="form.code"
              placeholder="会议编号"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开会时间" prop="openTime">
            <el-input
              v-model="form.openTime"
              placeholder="开会时间"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="主讲人" prop="speaker">
            <el-input
              v-model="form.speaker"
              placeholder="主讲人"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row  :gutter="20">
        <el-col :span="12">
          <el-form-item label="上限人数" prop="maxNum">
            <el-input
              v-model="form.maxNum"
              placeholder="上限人数"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { taskType, frequencyType } from "./types";
import BindDialog from '@/components/bindDialog/index.vue'
import { dialogFrame } from './frameData'
import { modifySeminar, addSeminar } from '@/api/seminar'
const markForm = () => {
  return {
    name: "",
    code: "",
    openTime: "",
    speaker: "",
    maxNum: 0
  }
}
export default {
  name: "",
  components: {
    BindDialog
  },
  data() {
    return {
      rules: {
        
      },
      title: "",
      type: "",
      config: {
        edit: {
          func: modifySeminar,
          title: "编辑"
        },
        new: {
          func: addSeminar,
          title: "新增"
        }
      },
      form: {
        name: "",
        code: "",
        openTime: "",
        speaker: "",
        maxNum: ""
      },
      visible: false,
      taskType,
      frequencyType,
      dialogFrame
    };
  },
  methods: {
    showDialog(row) {
      this.reset();
      const isEdit = !!row;
      this.visible = true;
      this.type = isEdit ? "edit" : "new";
      if (isEdit) {
        this.form = Object.assign(this.form, row);
        this.form.isMsg = this.form.isMsg == 1;
        this.form.isRepeat = this.form.isRepeat == 1;
        this.form.isComplete = this.form.isComplete == 1;
        this.form.frequency = this.form.frequency + ''
      }
      this.title = this.config[this.type].title;
    },

    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.visible = false;
          const data = { ...this.form }
          data.isMsg = data.isMsg ? 1 : 0;
          data.isRepeat = data.isRepeat ? 1 : 0;
          data.isComplete = data.isComplete ? 1 : 0;
          data.frequency = +data.frequency;
          if(data.num){
            delete data.num
          }
          if(data.isRepeat === 1){
            delete data.taskDate;
          }else{
            delete data.beginDate;
            delete data.endDate;
            delete data.frequency;
          }
          if(this.type === "new"){
            delete data.id;
            delete data.isComplete;
          }
          this.config[this.type]?.func(data).then(() => {
            this.$modal.msgSuccess("提交成功");
            this.visible = false;
            this.$emit("getTableList");
          });
        } else {
          return false;
        }
      });
    },

    cancel() {
      this.visible = false;
    },

    // 表单重置
    reset() {
      this.form = markForm();
      this.$forceUpdate();
    },

  },
};
</script>
<style scoped lang="scss">
  .person{
    position: relative;

    .el-icon-user{
      position: absolute;
      top: 10px;
      right: 10px;
      cursor: pointer;
    }

    .cover{
      width: 100%;
      height: 100%;
      position:absolute;
      top: 0;
      left: 0;
      z-index: 1;
      cursor: pointer;

      &:hover {
        .el-input__inner{
          border-color: #C0C4CC;
        }
      }
    }
  }
</style>
